package com.quyeying.controller;

import com.quyeying.annotation.PassToken;
import com.quyeying.entity.TblUserEntity;
import com.quyeying.service.TblUserService;
import com.quyeying.utils.TokenUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
* <p>
* tbl_user 前端控制器
* </p>
* @author skx
* @since 2022-07-15
*/
@Slf4j
@RestController
@RequestMapping("userEntity")
public class TblUserController {

    @Resource
    private TblUserService tblUserService;


    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(HttpServletRequest request,  @RequestBody TblUserEntity tblUserEntity){
        try {
            long userId = TokenUtil.getUserId(request);
            tblUserEntity.setId(userId);
            return tblUserService.updateData(tblUserEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 验证token是否有效
     */
    @PassToken
    @GetMapping("/verfiyToken")
    public R verfiyToken(HttpServletRequest request) {
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>(1);
        objectObjectHashMap.put("verfiyStatus", TokenUtil.verfiyToken(request));
        return R.success(objectObjectHashMap);
    }
}
